package com.wutong.subject_server.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wutong.common_server.result.Response;
import com.wutong.common_server.utils.BeanConvertUtils;
import com.wutong.common_server.utils.ResponseUtil;
import com.wutong.subject_server.entity.Subject;
import com.wutong.subject_server.feignInterface.TeacherServer;
import com.wutong.subject_server.param.SubjectParam;
import com.wutong.subject_server.service.SubjectService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.*;

/**
 * Create By Administrator on 2022/11/28.
 */
@RestController
@RequestMapping("/subject")
//@RefreshScope
@Api(tags = "课程相关接口")
public class SubjectController {

    @Value("${config.info}")
    private String configInfo;

    @Autowired
    SubjectService subjectService;
    @Autowired
    TeacherServer teacherServer;

    @ApiOperation(value = "添加课程接口",httpMethod = "POST")
    @PostMapping("/addSubject")
    public Response addSubject(@RequestBody SubjectParam subjectParam){

        Subject subject = BeanConvertUtils.convertTo(subjectParam, Subject::new);
        boolean b = subjectService.save(subject);
        if(b)return ResponseUtil.success("提交成功！");
        return ResponseUtil.failure("提交失败！");

    }
    @GetMapping("/getConfigInfo")
    private Response getConfigInfo(){

        return ResponseUtil.success("success","success",configInfo);
    }

    @PostMapping("/getTeachers")
    @ApiOperation("分页查询所有的课程")
    //单独对某个接口配置
    /*@HystrixCommand(fallbackMethod = "fallback2", commandProperties = {
            @HystrixProperty(name = "circuitBreaker.enabled", value = "true"),
            @HystrixProperty(name = "circuitBreaker.requestVolumeThreshold", value = "10"),
            @HystrixProperty(name = "circuitBreaker.sleepWindowInMilliseconds", value = "10000"),
            @HystrixProperty(name = "circuitBreaker.errorThresholdPercentage", value = "60"),
    })*/
    public Response getTeachers(@RequestBody SubjectParam subjectParam){

        Integer pageIndex = subjectParam.getPageIndex();
        Integer pageSize = subjectParam.getPageSize();
        if(pageIndex == null){
            pageIndex = 1;//默认查第一页
        }
        if(pageSize == null){
            pageSize = 10;//默认查10条数据
        }
        Page<Subject> page = new Page<>(pageIndex, pageSize);
        Page<Subject> subjectPage = subjectService.page(page);
        for (Subject subject : subjectPage.getRecords()) {
            String teacherId = subject.getTeacherId();
            String res = teacherServer.getTeacherById(teacherId);
            JSONObject data = JSONObject.parseObject(res).getJSONObject("data");
            if(data!=null) {
                String name = data.getString("name");
                subject.setTeacherName(name);
            }
        }
        return ResponseUtil.success(subjectPage);

    }

}
